package com.haois.mp.wechat.server;

import com.haois.mp.wechat.common.BusinessRemind;
import com.haois.mp.database.module.basic.util.SpringContextHolder;
import com.haois.mp.database.commom.enums.ApiResultEnum;
import com.haois.mp.database.commom.enums.MpTypeEnum;

import com.haois.mp.database.module.common.daomain.TempMessage;
import com.haois.mp.database.module.common.daomain.UserToken;
import com.haois.mp.database.module.remote.service.IUserService;
import com.haois.mp.database.module.wxserver.service.BaseMessageService;
import com.haois.mp.database.module.wxserver.util.MapUtil;
import com.haois.mp.database.module.wxserver.util.TempMsgUtil;
import com.haois.mp.database.module.common.service.IUserTokenService;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * description
 *
 * @author RuiHaoZ on GuangZhou
 * @date 2018/8/5 0:56
 */
@Component("TransferMsgServer")
public class TransferMsgServerImpl extends AbstractMoreInfoServer {
    @Autowired
    protected IUserService iUserService;
    @Autowired
    protected IUserTokenService iUserTokenService;
    @Autowired
    private BaseMessageService baseMessageService;


    @Override
    public String executePlus(WxMpXmlMessage wxMpXmlMessage, Object object) throws WxErrorException {
        WxMpService wxMpService = SpringContextHolder.getBean(WxMpService.class);
        UserToken userToken = (UserToken) object;
        String content = "";
        String texts[] = wxMpXmlMessage.getContent().split(" ");
        if (texts.length > 2) {
            UserToken receiveMember = iUserTokenService.findByUserId(texts[1], MpTypeEnum.OFFACCOUNTS.getType());
            if (receiveMember == null) {
                content = "该用户不存在或未绑定微信";
            } else {
                for (int i = 3; i <= texts.length - 1; i++) {
                    texts[2] += " " + texts[i];
                }
                Map<String, String> mapData = new HashMap<>();
                mapData.put("sender", userToken.getUserId());
                mapData.put("receiver", receiveMember.getUserId());
                mapData.put("content", texts[2]);
                mapData.put("subTit", texts[1]);

                if (texts[2].length() > 200) {
                    return "内容长度不能超过200字符";
                }
                String keFuMessage = "用户[" + userToken.getUserId() + "]\n发来消息:\n\n" + texts[2];
                if (BusinessRemind.remindByKeFu(wxMpService, receiveMember.getOpenId(), keFuMessage)) {
                    return "消息以客服消息形式发送成功";
                }
                if (texts[2].length() > 100) {
                    return "内容长度不能超过100字符";
                }

                TempMessage tempMessage = new TempMessage(mapData);
                tempMessage.setToUser(receiveMember.getOpenId());
                tempMessage.setFromUser(userToken.getOpenId());
                WxMpTemplate wxMpTemplate = BusinessRemind.getTemplateByTitle(wxMpService, TempMsgUtil.title_Fw);
                if (wxMpTemplate == null) {
                    return "转发消息失败,模板不存在";
                }
                tempMessage.setTempId(wxMpTemplate.getTemplateId());
                tempMessage.setTempName(wxMpTemplate.getTitle());

                Map map = this.sendMsgToUser(wxMpService, tempMessage);
                String state = "失败";
                if (map.get("code").equals("00")) {
                    state = "成功";
                }
                content = "消息以模板消息形式发送" + state;
            }
        } else {
            content = "转发消息请使用[转发 用户编号 消息内容]的格式";
        }
        return content;
    }

    public Map sendMsgToUser(WxMpService wxMpService, TempMessage tempMessage) throws WxErrorException {
        WxMpTemplateMessage tm = TempMsgUtil.createTemp(tempMessage, TempMsgUtil.temp_Fw, TempMsgUtil.temp_Default_Color);
        String reply = wxMpService.getTemplateMsgService().sendTemplateMsg(tm);
        baseMessageService.save(tm, tempMessage, reply);
        return MapUtil.createMap(ApiResultEnum.API_00.getCode(), "发送模板消息成功", reply);
    }
}
